package com.localizacion;


import java.util.Iterator;

import com.encuestas.*;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.location.GpsStatus;
import android.location.GpsStatus.Listener;
import android.location.GpsSatellite;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import android.util.Log;
import android.widget.Toast;

/**
 * @author jhovanny
 *
 */
/**
 * @author jhovanny
 *
 */
/**
 * @author jhovanny
 *
 */
/**
 * @author jhovanny
 *
 */
public class gps {

	private Double latitudgps, latitudmobil;
	private Double longitudgps, longitudmobil;
	private Double altitudmovil, altitudgps;
	LocationManager location;
	static AlertDialog aler=null;
	Context contesto;
	LocationListener listener, listenere;
	static int numerosatelites;


	


	/**
	 * @param el
	 *            contexto de la actividad actual
	 * @return verdadero cuando el gps esta encendido
	 * 
	 * 
	 * */

	
	public gps(Context cn)
	{
		
		contesto=cn;
		location = (LocationManager) contesto.getSystemService(Context.LOCATION_SERVICE);
		location.addGpsStatusListener(satelites);
		conectarmobil();
		conectargps();
		
		
	}
	Listener satelites= new GpsStatus.Listener(){

		@Override
		public void onGpsStatusChanged(int event) {
		GpsStatus status = location.getGpsStatus(null);
		
		if(status!=null)
		{
			Iterable<GpsSatellite> NumSatelites = status.getSatellites();
			Iterator<GpsSatellite> satelite = NumSatelites.iterator();
			numerosatelites=0;
			while(satelite.hasNext())
			{
				GpsSatellite sat = satelite.next();
			
				numerosatelites++;
				
			}
			
			
		}
		else
		{
			numerosatelites=0;		}
		
		
			
			
		}
		
		
		
		
	};
	
	 
	
	/**
	 * @return gps esta activido
	 * 
	 */
	public boolean checkGpsMobil()
	{
		return location.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
		
	}
	
	
	/**
	 * @return gps satelital disponible
	 */
	public boolean checkGpsSatelital()
	{
		return location.isProviderEnabled(LocationManager.GPS_PROVIDER);
		
	}
	
	

	public boolean conectarmobil() {
		
		location = (LocationManager) contesto
				.getSystemService(Context.LOCATION_SERVICE);
		listenere = new localli();

		
		if (location.isProviderEnabled(LocationManager.NETWORK_PROVIDER))

		{

			location.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
					60000, 0, listenere);
			return true;

		}

		else {

			return false;
		}

	}

	public boolean conectargps() {
	
		location = (LocationManager) contesto.getSystemService(Context.LOCATION_SERVICE);
		listener = new locali();

		if (location.isProviderEnabled(LocationManager.GPS_PROVIDER))

		{

			location.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0,
					listener);
			return true;
		}

		else {

			return false;
		}

	}

	public String oblatitudgps() {

		String latigps;

		if (latitudgps != null) {

			return latigps = Double.toString(latitudgps);

		} else

			return null;

	}

	public String oblatitudmobil() {

		String latimobil;

		if (latitudmobil != null)

		{

			return latimobil = Double.toString(latitudmobil);

		}

		else

			return null;

	}

	public String oblongitudgps() {

		String longigps;
		if (longitudgps != null) {

			return longigps = Double.toString(longitudgps);

		} else

			return null;

	}

	public String oblongitudmobil() {

		String longimobil;

		if (longitudmobil != null)

		{

			return longimobil = Double.toString(longitudmobil);

		}

		else

			return null;

	}

	public int numeroSatelites()
	{
		return numerosatelites;
	}
	public void deteneractualizaciones() {

		if (listener != null) {
			location.removeUpdates(listener);

		}

		if (listenere != null) {
			location.removeUpdates(listenere);
		}

	}

	public void actualizacionesmobiles() {

		if (conectarmobil()) {

			if (location.isProviderEnabled(LocationManager.NETWORK_PROVIDER))

			{

				// Location localmobil =
				// location.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
				location.requestLocationUpdates(
						LocationManager.NETWORK_PROVIDER, 60000, 0, listenere);
			
				/*
				 * if(localmobil!= null){
				 * 
				 * latitudmobil= localmobil.getLatitude(); longitudmobil=
				 * localmobil.getLongitude(); } else
				 * 
				 * {
				 * 
				 * latitudmobil= null; longitudmobil= null;
				 * 
				 * }
				 */

			}

		}
	}

	
	
	
	
	public void actualizacionesgps() {

		if (conectargps()) {
			if (location.isProviderEnabled(LocationManager.GPS_PROVIDER))

			{

				// Location localgps =
				// location.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				location.requestLocationUpdates(LocationManager.GPS_PROVIDER,
						60000, 0, listener);

				/*
				 * if (localgps!=null) { latitudgps= localgps.getLatitude();
				 * longitudgps= localgps.getLongitude(); } else
				 * 
				 * {
				 * 
				 * latitudgps= null; longitudgps= null;
				 * 
				 * }
				 */

			}

		}

	}

	
	
	
	
	private class locali implements LocationListener {
		public void onLocationChanged(Location location) {
			latitudgps = location.getLatitude();
			Log.i("LATITUD GP", "" + latitudgps);
			longitudgps = location.getLongitude();
			Log.i("LONGITUD GPS", "" + longitudgps);
			altitudgps=location.getAltitude();
			Log.i("altitud", "" + altitudgps);
		}

		
	
		public void onProviderDisabled(String provider) {
			//Toast.makeText(contesto,"El servicio de GPS satelital ha sido desactivado",Toast.LENGTH_SHORT).show();
			
			
			/*	Intent ir = new Intent(contesto, dialogos.class);
			
			ir.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
			contesto.startActivity(ir);*/
		
			/*Message msn = handel.obtainMessage();
			msn.what=1;
			handel.sendMessage(msn);*/
			
			
			Toast.makeText(contesto,
					"El servicio de GPS Satelital ha sido desactivado",
					Toast.LENGTH_SHORT).show();
	Intent ir = new Intent(contesto, dialogos.class);
			
			ir.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
			contesto.startActivity(ir);

			
			
			
			

		}

		public void onProviderEnabled(String provider) {
			Toast.makeText(contesto,
					"El servicio de GPS satelital ha sido activado",
					Toast.LENGTH_SHORT).show();
		

		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
			
			// TODO Auto-generated method stub

		}

	}


	
	
	private class localli implements LocationListener

	{

		public void onLocationChanged(Location location) {
			latitudmobil = location.getLatitude();
			Log.i("LATITUD MOVIL", "" + latitudmobil);
			longitudmobil = location.getLongitude();
			Log.i("LONGITUD MOVIL", "" + longitudmobil);
			altitudmovil = location.getAltitude();
			Log.i("ALTITUD", "" + altitudmovil);
		}

		public void onProviderDisabled(String provider) {

			Toast.makeText(contesto,
					"El servicio de GPS asistido ha sido desactivado",
					Toast.LENGTH_SHORT).show();
		
			

		}

		public void onProviderEnabled(String provider) {
			Toast.makeText(contesto,
					"El servicio de GPS asistido ha sido activado",
					Toast.LENGTH_SHORT).show();
	Intent ir = new Intent(contesto, dialogos.class);
			
			ir.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
			contesto.startActivity(ir);

		}

		public void onStatusChanged(String provider, int status, Bundle extras) {
		

		}

	}

	



}
